package com.hs.excel.demos.service.transform;

import com.hs.excel.demos.listenter.Comont;
import com.hs.excel.demos.pojo.RequestParm;
import com.hs.excel.demos.service.transform.Transform;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MysqlTransform {

   public void oneInsert(RequestParm requestParm, Map<Integer, String> head,Map<String, String> data){
       StringBuffer  stringBuffer = new StringBuffer("insert into");
       stringBuffer.append(" " + requestParm.getTableName() + "(");

       List<String> heads = new ArrayList<>(head.values());
       for (String h : heads) {
           String s = h + ",";
           stringBuffer.append(s);
       }
       stringBuffer.deleteCharAt(stringBuffer.length() - 1);
       stringBuffer.append(") ");
       stringBuffer.append("VALUES(");
       List<String> values = new ArrayList<>(data.values());
       for (String filed : values) {
           if (filed == null) {
               String s = "'',";
               stringBuffer.append(s);
           } else {
               String s = "'" + filed + "'" + ",";
               stringBuffer.append(s);
           }
       }
       stringBuffer.deleteCharAt(stringBuffer.length() - 1);
       stringBuffer.append("); ");
       Comont.insertList.add(stringBuffer.toString());
   }

    public void entiretyMysqlhandle(Map<String, String> data) {
        //单条
        List<String> values = new ArrayList<>(data.values());

        StringBuffer  stringBuffer = new StringBuffer("(");
        for (String filed : values) {
            if (filed == null) {
                String s = "'',";
                stringBuffer.append(s);
            } else {
                String s = "'" + filed + "'" + ",";
                stringBuffer.append(s);
            }
        }
        stringBuffer.deleteCharAt(stringBuffer.length() - 1);
        stringBuffer.append("),");
        Comont.insertList.add(stringBuffer.toString());
    }


    public void entiretyMysqlEnd(Map<Integer, String> head, RequestParm requestParm) {
        String s1 = Comont.insertList.get(Comont.insertList.size() - 1);
        String s2 = s1.substring(0, s1.length() - 1) + ';';
        // 获取最后一个元素的索引
        int lastIndex = Comont.insertList.size() - 1;
        // 替换最后一个元素
        Comont.insertList.set(lastIndex, s2);
        StringBuilder stringBuilder = new StringBuilder("insert into");
        stringBuilder.append(" " + requestParm.getTableName() + "(");
        List<String> heads = new ArrayList<>(head.values());
        for (String h : heads) {
            String s = h + ",";
            stringBuilder.append(s);
        }
        stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        stringBuilder.append(") ");
        stringBuilder.append("VALUES");
        Comont.insertList.add(0, stringBuilder.toString());
    }

}
